package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.Dao.base.ICourierDao;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.ICourierService;

/** 
 * 快递员的业务实现层
 *
 * @author Shao-Fei Fan 
 * @version 1.0,2017年9月3日 下午8:34:26 
 */
@Service
@Transactional
public class CourierServiceImpl implements ICourierService {
	
	@Autowired
	private ICourierDao courierDao;
	
	
	/**
	 * @see cn.itcast.bos.service.base.ICourierService#saveCourier(Courier)
	 */
	@Override
	public void saveCourier(Courier c) {
		courierDao.save(c);
	}
	
	/**
	 * @see cn.itcast.bos.service.base.ICourierService#showCourierByPage()
	 */
	@Override
	public Page<Courier> showCourierByPage(Specification<Courier> specification, Pageable pageable) {
		Page<Courier> all = courierDao.findAll(specification,pageable);
		
		return all;
	}
	
	/**
	 * @see cn.itcast.bos.service.base.ICourierService#deletCourier(int)
	 */
	@Override
	public void deletCourier(Integer id) {
		courierDao.deletCourier(id);
	}
	
	/**
	 * @see cn.itcast.bos.service.base.ICourierService#restoreCourier(Integer)
	 */
	@Override
	public void restoreCourier(Integer id) {
		courierDao.restoreCourier(id);
	}
	
	/**
	 * @see cn.itcast.bos.service.base.ICourierService#findnoAssociationFixedArea()
	 */
	@Override
	public List<Courier> findnoAssociationFixedArea() {
		
		// 构造查询条件
		Specification<Courier> specification = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
		};
		List<Courier> findAll = courierDao.findAll(specification);
		return findAll;
	}

}
